// packageA/pages/address-add/index.js
const WXAPI = require('../../../api/api')
const App = getApp()
import valid from '../../utils/valid'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    areaInfoSelected: {
      provinceObj: {
        sCode: '',
        sName: ''
      },
      cityObj: {
        sCode: '',
        sName: ''
      },
      areaObj: {
        sCode: '',
        sName: ''
      },
      streetObj: {
        sCode: '',
        sName: ''
      },
      villageObj: {
        sCode: '',
        sName: ''
      },
    },
    sTagCode:'',
    sTagName:'',
    sName: '',
    sPhone: '',
    area: {},
    sAddress: '',
    phoneValuate: true,
    isSubmit: false,
    iDefaultAddress: 1,
    tipOptions: [],
    id:''
  },
  selectCurrentTip(event) {
    console.log('event',event);
    let arr = JSON.parse(JSON.stringify(this.data.tipOptions))
    for (let i = 0; i < arr.length; i++) {
      if (arr[i].sValue == event.currentTarget.dataset.id) {
        arr[i].isCheck = true
      } else {
        arr[i].isCheck = false
      }
    }
    this.setData({
      tipOptions: arr,
      sTagCode: event.currentTarget.dataset.id,
      sTagName: event.currentTarget.dataset.name
    })
    console.log('arr', this.data.tipOptions);
  },
  changeSwitch(event) {
    this.setData({
      iDefaultAddress: this.data.iDefaultAddress == 1 ? 0 : 1
    })
  },
  async saveAddress() {
    this.setData({
      isSubmit: true
    })
    console.log('this.data',this.data);
    // 手动校验必填项
    if(this.data.sName && this.data.sPhone && this.data.area.sProvinceCode && this.data.sAddress && this.data.sTagCode){
      const { sName,sPhone,sAddress,iDefaultAddress,sTagCode,sTagName } = this.data
      let data = {
        ...this.data.area,
        sName,
        sPhone,
        sAddress,
        iDefaultAddress,
        sTagCode,
        sTagName
      }
      let res
      if(this.data.id) {
         res = await WXAPI.editMyAddress(this.data.id,data)
      } else {
         res = await WXAPI.saveMyAddress(data)
      }
     
      if(res.data.code == 0) {
        wx.showToast({
          title: res.data.message
        })
        wx.navigateBack({
          delta: 1
        })
      }
    }
    
  },
  async sureAddress(val) {
    console.log('val',val);
    let {
      sProvinceCode,
      sProvinceName,
      sCityCode,
      sCityName,
      sCountryCode,
      sCountryName,
      sStreetCode,
      sStreetName,
      sCommunityCode,
      sCommunityName
    } = val.detail.obj
    this.setData({
      ["area.sProvinceCode"]: sProvinceCode,
      ["area.sProvinceName"]: sProvinceName,
      ["area.sCityCode"]: sCityCode,
      ["area.sCityName"]: sCityName,
      ["area.sCountryCode"]: sCountryCode,
      ["area.sCountryName"]: sCountryName,
      ["area.sStreetCode"]: sStreetCode,
      ["area.sStreetName"]: sStreetName,
      ["area.sCommunityCode"]: sCommunityCode,
      ["area.sCommunityName"]: sCommunityName
    })
    console.log('area', this.data.area);
    // let res = await WXAPI.saveMyAddress()
  },
  async getTags(){
    let dictList = JSON.parse(JSON.stringify(App.globalData.dictTree))
    // console.log('dictList',dictList);
    // 默认选中第一个
    dictList.filter(item=>item.sValue == "AddressTag")[0].Child[0].isCheck = true
    this.setData({
      sTagCode:dictList.filter(item=>item.sValue == "AddressTag")[0].Child[0].sValue,
      sTagName:dictList.filter(item=>item.sValue == "AddressTag")[0].Child[0].sName,
      tipOptions: dictList.filter(item=>item.sValue == "AddressTag")[0].Child
    })
  },
  async getAddressById(id){
    let res = await WXAPI.getAddressInfo(id)
    let {sTagCode,sTagName,sName,sPhone,sProvinceCode,sProvinceName,sCityCode,sCityName,sCountryCode,sCountryName,sStreetCode,sStreetName,sCommunityCode,sCommunityName,sAddress,iDefaultAddress} = res.data.data
    if(res.data.code == 0) {
      let dictList = JSON.parse(JSON.stringify(App.globalData.dictTree))
      dictList.filter(item=>item.sValue == "AddressTag")[0].Child.filter(item2=>item2.sValue == sTagCode)[0].isCheck = true
      console.log('dictList',dictList);
      this.setData({
        sTagCode,
        sTagName,
        sName,
        sPhone,
        ["area.sProvinceCode"]: sProvinceCode,
        ["area.sProvinceName"]: sProvinceName,
        ["area.sCityCode"]: sCityCode,
        ["area.sCityName"]: sCityName,
        ["area.sCountryCode"]: sCountryCode,
        ["area.sCountryName"]: sCountryName,
        ["area.sStreetCode"]: sStreetCode,
        ["area.sStreetName"]: sStreetName,
        ["area.sCommunityCode"]: sCommunityCode,
        ["area.sCommunityName"]: sCommunityName,
        sAddress,
        iDefaultAddress,
        tipOptions: dictList.filter(item=>item.sValue == "AddressTag")[0].Child,
        ["areaInfoSelected.provinceObj.sCode"]:sProvinceCode,
        ["areaInfoSelected.provinceObj.sName"]:sProvinceName,
      
      })
      console.log('areaInfoSelected',this.data.areaInfoSelected);
    }
  },
  phoneNumberValid(){
    this.setData({
      phoneValuate: valid.validatePhoneOrNum(this.data.sPhone) 
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.id) {
      this.setData({
        id:options.id
      })
      wx.setNavigationBarTitle({
        title: '修改地址',
      })
      this.getAddressById(options.id)
    } else {
      wx.setNavigationBarTitle({
        title: '添加地址',
      })
    }
    this.getTags()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})